package main;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

	/**
	 * 输入日期模式并把date日期对象转换为日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getStringDate(Date date, String pattern) {
		try {
			SimpleDateFormat f = new SimpleDateFormat(pattern);
			String strdate = f.format(date);
			return strdate;
		} catch (Exception pe) {
		}
		return null;
	}
	
	/**
	 * 输入日期模式并把date日期对象转换为日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getYestoday(String pattern) {
		try {
			SimpleDateFormat f = new SimpleDateFormat(pattern);
			String strdate = f.format(getDateBefore(new Date(),1));
			return strdate;
		} catch (Exception pe) {
		}
		return null;
	}
	
	/**
	 * 将String时间转为Date格式，按照指定格式。
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate, String patter) {
		SimpleDateFormat formatter = new SimpleDateFormat(patter);
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	   
	public static String strToStr(String strDate,String inPattern, String outPattern) {
		Date tmpDate=strToDate(strDate,inPattern);
		return getStringDate(tmpDate, outPattern);
	}

	
	public static String strToStrBefore(String strDate,String patter,int day){
		Date tmpDate=strToDate(strDate,patter);
		tmpDate=getDateBefore(tmpDate,day);
		return getStringDate(tmpDate,patter);
	}
	
	public static String strToStrAfter(String strDate,String patter,int day){
		Date tmpDate=strToDate(strDate,patter);
		tmpDate=getDateAfter(tmpDate,day);
		return getStringDate(tmpDate,patter);
	}
	
		/** @author zhaoxiang
	    * 获得到几天前的时间
	    * @param d 
	    * @param day 
	    * @return 
	    */ 
	 public static Date getDateBefore(Date d, int day) {  
	       Calendar now = Calendar.getInstance();  
	       now.setTime(d);  
	       now.add(Calendar.DATE, -day);  
	       return now.getTime();  
	   }  
	   
		/** 
	    *  
	    * 获得到几天后的时间
	    * @param d 
	    * @param day 
	    * @return 
	    */  
	   public static Date getDateAfter(Date d, int day) {  
	       Calendar now = Calendar.getInstance();  
	       now.setTime(d);  
	       now.set(Calendar.DATE, now.get(Calendar.DATE) + day);  
	       return now.getTime();  
	   }  
			   

			/**根据起始时间获取其所有日期段 , 返回格式为yyyyMMdd,类似天表名后缀
			 * @param startDate
			 * @param endDate
			 * @return
			 */
			public static List<String> getDayList(String startDate,String endDate){
				
	        	List<String>  dateList = new ArrayList<String>();
				while(true){
					dateList.add(startDate);
					if(startDate.equals(endDate)){
						break;
					}
					startDate = DateUtil.strToStrAfter(startDate, "yyyyMMdd" , 1);
				}
				
				return dateList;
			}
			
			
}
